package com.example.demo.entity;


import com.example.demo.factory.StudentManageFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SmartCard {

    private StudentName studentName;

    private Date studentBirthday;

    private Date issueDate;

    private SmartCardId smartCardId;

    private Date expiryDate;

    public StudentName getStudentName() {
        return studentName;
    }

    public void setStudentName(StudentName studentName) {
        this.studentName = studentName;
    }

    public Date getStudentBirthday() {
        return studentBirthday;
    }

    public void setStudentBirthday(Date studentBirthday) {
        this.studentBirthday = studentBirthday;
    }

    public Date getIssueDate() {
        return issueDate;
    }

    public void setIssueDate(Date issueDate,String type) {
        this.issueDate = issueDate;
        setExpiryDate(issueDate,type);
    }

    public SmartCardId getSmartCardId() {
        return smartCardId;
    }

    public void setSmartCardId(SmartCardId smartCardId) {
        this.smartCardId = smartCardId;
    }

    public Date getExpiryDate() {
        return expiryDate;
    }

    private void setExpiryDate(Date issueDate,String type) {
        SimpleDateFormat sdf = new SimpleDateFormat(StudentManageFactory.YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(issueDate);
        Date expiryDate = null;
        if ("UG".equals(type)) {
            calendar.add(Calendar.YEAR,4);
        } else if ("PGT".equals(type)) {
            calendar.add(Calendar.YEAR,2);
        } else if ("PGR".equals(type)) {
            calendar.add(Calendar.YEAR,5);
        }
        try {
            expiryDate = sdf.parse(sdf.format(calendar.getTime()));
        } catch (ParseException e) {
            System.out.println("日期转换失败");
        }
        this.expiryDate = expiryDate;
    }

    @Override
    public String toString() {
        return "SmartCard{" +
                "studentName=" + studentName.toString() +
                ", studentBirthday=" + studentBirthday +
                ", issueDate=" + issueDate +
                ", smartCardId=" + smartCardId +
                ", expiryDate=" + expiryDate +
                '}';
    }
}
